<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路：
            pre  1  2  3  4
            第一步：先让pre.next指向要交换的头部，然后这一组进行交换
            保存2，让1指向3，然后让2指向1 实现交换pre   2      1     3 4    再更新pre和cur
                                                      next    cur
            */

            //如果没有虚拟头节点，就没法保证链表连接了
            var swapPairs = function (head) {
                let dummy = new ListNode(0, head)
                let cur = head
                let next = null
                let pre = dummy
                while (cur && cur.next) {
                    next = cur.next
                    pre.next = next
                    cur.next = next.next
                    next.next = cur
                    //更新pre和cur
                    pre = cur
                    cur = cur.next
                }
                return dummy.next
            }
        </script>
    </body>
</html>
